"""
    自定义BootStrap的ModelForm组件作为基类
"""
from django.forms import models


class BootStrapModelForm(models.ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)  # 执行父类初始化命令

        # 循环ModelForm中的所有字段，给每个字段设置属性值
        for name, field in self.fields.items():
            # 如果字段中有属性，就保留原来的属性，没有属性，才增加
            if field.widget.attrs:
                field.widget.attrs["class"] = "form-control"
                field.widget.attrs["placeholder"] = field.label
            else:
                field.widget.attrs = {
                    "class": "form-control",
                    "placeholder": field.label
                }
            # if name == 'password':  # 不给password加
            #     field.widget.attrs = {"class": "form-control", 'type': 'password'}
